﻿Imports System.Collections.Generic

Partial Class WebForm_SearchControl_SearchRegionVolunteerControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Event OK_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent OK_Click(Me, New EventArgs)
        Me.mpSearch.Show()
        GridView_Volunteers.DataBind()
    End Sub
    'Protected Sub cbCheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    '    Dim chkAll As CheckBox = TryCast(sender, CheckBox)

    '    For Each row As GridViewRow In Me.GridView_Volunteers.Rows
    '        Dim chk As CheckBox = TryCast(row.FindControl("CheckBox1"), CheckBox)

    '        chk.Checked = chkAll.Checked
    '    Next

    'End Sub
    Protected Sub GridView_Volunteers_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_Volunteers.PageIndexChanged
        Me.mpSearch.Show()
    End Sub

    Protected Sub GridView_Volunteers_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_Volunteers.Sorted
        Me.mpSearch.Show()
    End Sub

    '查詢
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click

        If txtVolunteerId.Text = String.Empty Then

            ObjectDataSource1.SelectParameters("Region").DefaultValue = ddlRegion.SelectedValue
            ObjectDataSource1.SelectParameters("Region2").DefaultValue = ddlRegion2.SelectedValue

        Else

            ObjectDataSource1.SelectParameters("Region").DefaultValue = String.Empty
            ObjectDataSource1.SelectParameters("Region2").DefaultValue = String.Empty

        End If

        If Me.ddlRegion.Items.Count = 0 And Me.txtVolunteerId.Text = "" Then
            MyObj.ShowMessage("身份證號未輸入")
            Return
        End If

        ObjectDataSource1.SelectParameters("VolunteerId").DefaultValue = txtVolunteerId.Text
        ObjectDataSource1.SelectParameters("VolunteerName").DefaultValue = txtVolunteerName.Text
        ObjectDataSource1.SelectParameters("TEL").DefaultValue = txtTEL.Text

        ObjectDataSource1.SelectParameters("VOrganizationID").DefaultValue = VOrganizationID

        ObjectDataSource1.SelectParameters("NoOrgVolunteer").DefaultValue = Me.chkNoOrgVolunteer.Checked

        GridView_Volunteers.DataSourceID = ObjectDataSource1.ID
        GridView_Volunteers.DataBind()

        Me.mpSearch.Show()
    End Sub

    Protected Sub ddlRegion_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegion.DataBound
        If ddlRegion.Items.Count = 0 Then
            Me.trRegion.Visible = False
        ElseIf ddlRegion.Items.Count > 1 Then
            ddlRegion.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))
        End If
        'ddlRegion.SelectedIndex = 0

    End Sub

    Protected Sub ddlRegion2_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegion2.DataBound
        If ddlRegion2.Items.Count > 1 Then
            ddlRegion2.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))
        End If
    End Sub

    Protected Sub ddlRegion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegion.SelectedIndexChanged
        ddlRegion2.DataSource = clsAuth.GetVillageData(ddlRegion.SelectedValue)
        ddlRegion2.DataBind()

        Me.mpSearch.Show()
    End Sub

End Class

Partial Class WebForm_SearchControl_SearchRegionVolunteerControl

    Public Sub InitDataAndShow(ByVal OrganizationID As Integer)

        'chk_ddlRegion.ValidationGroup = ValidationGroup
        'btnQuery.ValidationGroup = ValidationGroup

        ddlRegion.DataSource = clsAuth.GetTownData()
        ddlRegion.DataBind()
        ddlRegion2.DataSource = clsAuth.GetVillageData(ddlRegion.SelectedValue)
        ddlRegion2.DataBind()

        Me.txtVolunteerId.Text = ""
        Me.txtVolunteerName.Text = ""
        Me.txtTEL.Text = ""

        GridView_Volunteers.DataSourceID = ""

        ObjectDataSource1.SelectParameters("OrganizationID").DefaultValue = OrganizationID

        Me.mpSearch.Show()

    End Sub

    Public ReadOnly Property SelectedValues() As List(Of String)
        Get
            Dim Volunteers As New List(Of String)

            For Each row As GridViewRow In Me.GridView_Volunteers.Rows
                Dim key As DataKey = Me.GridView_Volunteers.DataKeys(row.RowIndex)

                If TryCast(row.FindControl("CheckBox1"), CheckBox).Checked = True Then
                    Volunteers.Add(key("VolunteerID"))
                End If
            Next

            Return Volunteers
        End Get
    End Property

    Public Property VOrganizationID() As String
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set
    End Property

End Class